// const json = require("./mtxf6e53bv.json")
const fs = require("fs")
const ExcelJS = require('exceljs');
const lodash = require('lodash')

// var str = fs.readFileSync("./json.json", 'utf-8')
// var json = str.split("\n")
var json =require("./json.json")
// var max = json.length;




async function ajaxIinit(start,max) {
    var json500 = []
    for (let i = start; i < max; i++) {
        try{
            var item=JSON.parse(json[i])._source
            console.log(JSON.parse(json[i])._source.id)
            json500.push(item)
        }catch (e) {
            console.log(e)
        }

    }
    const workbook = new ExcelJS.Workbook();
    workbook.creator = 'Me';
    workbook.lastModifiedBy = 'Her';
    workbook.created = new Date(1985, 8, 30);
    workbook.modified = new Date();
    workbook.lastPrinted = new Date(2016, 9, 27);

    const worksheet = workbook.addWorksheet(`mysheet`);


    worksheet.columns = [
        {header: 'id', key: 'id', width: 32},
        {header: 'ip', key: 'ip', width: 32},
        {header: 'osName', key: 'osName', width: 32},
        {header: 'browserName', key: 'browserName', width: 32},
        {header: 'deviceType', key: 'deviceType', width: 32},
        {header: 'country', key: 'country', width: 32},
        {header: 'province', key: 'province', width: 32},
        {header: 'city', key: 'city', width: 32},
        {header: 'domain', key: 'domain', width: 32},
        {header: 'url', key: 'url', width: 32},
        {header: 'referrer', key: 'referrer', width: 32},
        {header: 'title', key: 'title', width: 32},
        {header: 'screenRatio', key: 'screenRatio', width: 32},
        {header: 'screenColorDepth', key: 'screenColorDepth', width: 32},
        {header: 'language', key: 'language', width: 32},
        {header: 'isp', key: 'isp', width: 32},
        {header: 'createTime', key: 'createTime', width: 32},
        {header: 'todayVisits', key: 'todayVisits', width: 32},
        {header: 'fingerprint', key: 'fingerprint', width: 32},
        {header: 'siteId', key: 'siteId', width: 32},
        {header: 'eventId', key: 'eventId', width: 32},
        {header: 'eventType', key: 'eventType', width: 32},
        {header: 'keywords', key: 'keywords', width: 32},
        {header: 'origin', key: 'origin', width: 32},
        {header: 'originType', key: 'originType', width: 32},
        {header: 'siteUrl', key: 'siteUrl', width: 32},
        {header: 'siteTime', key: 'siteTime', width: 32},
        {header: 'originDomain', key: 'originDomain', width: 32}
    ];

    json500.map(item => {
        worksheet.addRow(item);
    })
    await workbook.xlsx.writeFile(`D:\\workplace\\xgyz_dongbohui\\json2excel\\result${start}_${parseInt(max)}.xlsx`);
}


async function  init(){
    let isBreak=true;
    var length=json.length;
    var count=0;
    while (isBreak){
        var start=count*8000
        var max=start+8000
        count++;
        console.log("=====================================start end",start,max)
        await  ajaxIinit(start,max)
        if(max>length){
            isBreak=false;
        }
    }

    console.log("总length===========>",length)

}

init()